package com.tbit.main.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tbit.common.entity.UserOpLog;
import com.tbit.common.utils.TraceUtil;
import com.tbit.main.dao.log.UserOpLogDao;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

/**
 * @author <a href="mailto:zheng.zebiao@tbit.com.cn">zb</a>
 * @since 2024/11/27
 */
@Slf4j
@Service
public class UserOpLogService extends ServiceImpl<UserOpLogDao, UserOpLog> {

    public void insertSingle(UserOpLog userOpLog) {
        try {
            StringBuilder detail = new StringBuilder(ObjectUtil.defaultIfNull(userOpLog.getDetail(), ""));
            String traceId = TraceUtil.getTraceIdByLocal();
            if (traceId != null) {
                if (detail.length() > 0) {
                    detail.append("\n");
                }
                detail.append("TraceID: ")
                        .append(traceId);
            }
            userOpLog.setDetail(detail.length() > 0 ? detail.toString() : null);
            getBaseMapper().insert(userOpLog);
        } catch (Exception e) {
            log.error("[人员操作日志] 插入失败: {}", userOpLog, e);
        }
    }
}
